package com.qingke.business;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.ibatis.session.SqlSession;

import com.qingke.Command.UserCommand;
import com.qingke.mapper.RecordeMapper;
import com.qingke.memo.MemoConsole;
import com.qingke.memo.MemoDao;
import com.qingke.sqlclass.Recorde;

public class UpdateCommand extends UserCommand{

	@Override
	public void executeUserCommand() {
		System.out.println("欢迎进入备忘录修改页面：");
		SqlSession session = MemoDao.mybatisSession();
		RecordeMapper recordeM = session.getMapper(RecordeMapper.class);
		System.out.println("请输入你要修改的id(可先list了解一下id)");
		int id;
		while(true){
			id = Integer.parseInt(MemoConsole.askUserInput("cmd >"));
			if(recordeM.selectOne(id)==null){
				System.out.println("输入的id不正确，请重新输入！");
			}else {
				break;
			}
		}
		String s = null ;
		while(true){
			String title = MemoConsole.nullUserInput("请输入标签【1.工作】【2.生活】【3.业余】请输入数字：>:");
			if(Integer.parseInt(title)>0&&Integer.parseInt(title)<4){
				System.out.println("请重新输入！");
			}else{
				s = title;
				break;
			}
		}
		String description = MemoConsole.nullUserInput("请输入要修改的description（无则回车）:");
		String	remind_time = MemoConsole.askUserInput("请输入remind_time:");
		DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date myDate2 = null;
		try {
			myDate2 = dateFormat2.parse(remind_time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar date = Calendar.getInstance();
		int year = date.get(Calendar.YEAR);
		int month = date.get(Calendar.MONTH) + 1;
		int day = date.get(Calendar.DAY_OF_MONTH);
		int hour = date.get(Calendar.HOUR_OF_DAY); 
		int minute = date.get(Calendar.MINUTE); 
		int second = date.get(Calendar.SECOND);
		String create_time = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
		Date myDate3 = null;
		try {
			myDate3 = dateFormat2.parse(create_time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Recorde recorde = new Recorde();
		recorde.setId(id);
		recorde.setTitle(s);
		recorde.setDescription(description);
		recorde.setRemindTime(myDate2);
		recorde.setCreateTime(myDate3);
		recordeM.updateByPrimaryKeySelective(recorde);
		session.commit();
		System.out.println("修改成功！");
		
	}
}
